<script lang="ts" setup>
import type { SourceTrackCategoryVO } from '#/api/config/trackCategory/model';

import { onMounted, ref } from 'vue';

import { Select, Spin } from 'ant-design-vue';

import { sourceTrackCategoryList } from '#/api/config/trackCategory';

// interface Props {
//   modelName?: 'id' | 'name';
// }
//
// const props = withDefaults(defineProps<Props>(), {
//   modelName: 'name',
// });

const emit = defineEmits(['blur', 'change', 'select']);
const loading = ref(false);
const list = ref<SourceTrackCategoryVO[]>([]);
const modelValue = defineModel<number | string>('value', {
  default: () => undefined,
});

onMounted(async () => {
  loading.value = true;
  // 查询状态为可用的分类
  const results = await sourceTrackCategoryList({ status: '0' });
  list.value = results || [];
  loading.value = false;
});

function handleChange(val: any) {
  console.log(val);
  emit('change', modelValue.value);
}
</script>
<template>
  <Select
    v-model:value="modelValue"
    class="w-full"
    placeholder="请选择"
    show-search
    :options="list"
    option-filter-prop="name"
    :field-names="{ label: 'name', value: 'id' }"
    @blur="emit('blur')"
    @change="handleChange"
  >
    <template v-if="loading" #notFoundContent>
      <Spin size="small" />
    </template>
  </Select>
</template>
